import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  RestDto,
  ENUM_APP,
  StockNode,
  getNotchByIVIDRpcDto,
  resetOrderStockRpcDto,
  putStockNodeRootRpcDto,
  RiverStockRpcPutRootPath,
  RiverStockNodeRpcInterface,
  RiverStockNotchRpcInterface,
  RiverStockRpcResetOrderStockPath,
  RiverStockRpcGetNotchByIVIDPath,
  RiverStockRpcDoStockNotchBusyCheckPath,
  RiverStockRpcCopyPath,
  copyStockNodeLinkRpcDto,
  RiverStockRpcGetNodeTreePath,
  getStockNodeRpcDto,
  getOrderGroupRootNodeRpcDto,
  RiverStockRpcGetOrderGroupRootNodePath,
  getRootNodeUpperOrderGroupRpcDto,
  RiverStockRpcGetRootNodeUpperOrderGroupPath,
  TonOrderGroup,
  PutRes,
  CommonStock,
  RiverStockRpcResetCommonStockPath,
  resetCommonStockRpcDto,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
export class RiverStockRpc extends RpcConnection implements RiverStockNodeRpcInterface<RestDto>, RiverStockNotchRpcInterface<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.RIVER_STOCK);
  }

  @RpcWaiting(RiverStockRpcGetNodeTreePath)
  async getNodeTree(dto: RestDto & getStockNodeRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcGetNodeTreePath, dto);
    return lastValueFrom<StockNode[][]>(ob);
  }

  @RpcWaiting(RiverStockRpcPutRootPath)
  async putRootSync(dto: RestDto & putStockNodeRootRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcPutRootPath, dto);
    return lastValueFrom<PutRes<StockNode>>(ob);
  }

  @RpcWaiting(RiverStockRpcCopyPath)
  async copy(dto: RestDto & copyStockNodeLinkRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcCopyPath, dto);
    return lastValueFrom<null>(ob);
  }

  @RpcWaiting(RiverStockRpcGetNotchByIVIDPath)
  async getNotchByIVID(dto: RestDto & getNotchByIVIDRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcGetNotchByIVIDPath, dto);
    return lastValueFrom<{ list: CommonStock[] }>(ob);
  }

  @RpcWaiting(RiverStockRpcGetOrderGroupRootNodePath)
  async getOrderGroupRootNode(dto: RestDto & getOrderGroupRootNodeRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcGetOrderGroupRootNodePath, dto);
    return lastValueFrom<{ rootStockNodeLs: StockNode[] }>(ob);
  }

  @RpcWaiting(RiverStockRpcGetRootNodeUpperOrderGroupPath)
  async getRootNodeUpperOrderGroup(dto: RestDto & getRootNodeUpperOrderGroupRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcGetRootNodeUpperOrderGroupPath, dto);
    return lastValueFrom<{ groupIDs: TonOrderGroup["id"][] }>(ob);
  }

  //

  @RpcWaiting(RiverStockRpcDoStockNotchBusyCheckPath)
  async doBusyCheck(dto: RestDto & { maxTask?: number }) {
    const ob = this.CLIENT.send(RiverStockRpcDoStockNotchBusyCheckPath, dto);
    return lastValueFrom<{ count: number }>(ob);
  }

  @RpcWaiting(RiverStockRpcResetOrderStockPath)
  async resetOrderStock(dto: RestDto & resetOrderStockRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcResetOrderStockPath, dto);
    return lastValueFrom<void>(ob);
  }

  @RpcWaiting(RiverStockRpcResetCommonStockPath)
  async resetCommonStock(dto: RestDto & resetCommonStockRpcDto) {
    const ob = this.CLIENT.send(RiverStockRpcResetCommonStockPath, dto);
    return lastValueFrom<void>(ob);
  }
}
